package com.cgt.xiaotao.product.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.cgt.common.utils.R;
import com.cgt.xiaotao.product.domain.query.ProductPageQuery;
import com.cgt.xiaotao.product.domain.vo.ProductListVO;
import com.cgt.xiaotao.product.service.ISearchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Api(tags = "搜索接口")
@Slf4j
@RestController
@RequestMapping("/search")
@RequiredArgsConstructor
public class SearchController {

    private final ISearchService searchService;

    @ApiOperation("分页查询商品列表(标签条件查询)")
    @PostMapping("/items")
    public R<PageDTO<ProductListVO>> getPage(@RequestBody ProductPageQuery query) {
        try {
            PageDTO<ProductListVO> pageDTO = searchService.selectPageByCondition(query);
            return R.ok(pageDTO);
        }catch (Exception e) {
            log.error("分页查询失败: {}", e.getMessage(), e);
            return R.fail("查询失败");
        }
    }

    @ApiOperation("根据关键字搜索商品名称建议")
    @GetMapping("/suggest")
    public R<String[]> suggest(@RequestParam String keyword) {
        try {
            String[] suggests = searchService.getSuggest(keyword);
            return R.ok(suggests);
        }catch (Exception e) {
            log.error("查询失败: {}", e.getMessage(), e);
            return R.fail("查询失败");
        }
    }

}
